﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;
using CMS.BLL.Entity;

public partial class AdminX_Banner_BannerMain : System.Web.UI.UserControl
{
    private int nDefaulanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ShowMess();

            this.BindDataToLanguageDropDown();
            this.SetMullanguage();
           
            this.BindDataToGrid(1);
        }
    }

    private void SetMullanguage()
    {
       
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaulanguage - 1];      
        btnAddnew.Text = MultipleLanguage.BTN_ADDNEW[nDefaulanguage - 1];
        
    }

    #region Paging & Message
   

    private void ShowMess()
    {
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        int nDefaultLang = Support.GetDefaultLanguage() - 1;
        switch (nMessage)
        {
            case 1:
                MessageBoxss.Show(MessagesAlert.INSERT_SUCCESSFULL[nDefaultLang]);
                break;
            case 2:
                MessageBoxss.Show(MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLang]);
                break;
            case 3:
                MessageBoxss.Show(MessagesAlert.ITEM_NOTEXIST[nDefaultLang]);
                break;
            default:

                break;
        }
    }
    #endregion

    #region BindDataToDropDown

    private void textCreateMarginLeft(ref string text, Int16 count)
    {
        if (count > 0)
        {
            string sMargin = String.Empty;
            for (int i = 0; i < count; i++)
            {
                sMargin += "--";
                sMargin += " ";
            }
            text = sMargin + text;
        }
    }

    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }
    #endregion

    #region Grid processing

    protected void RpBanner_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Banner objBanner = new Banner();
        switch (strCommand)
        {
            case "Delete":
                int nDelete = objBanner.DeleteById(nID);
                //if (nDelete > 0)
                //    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[1]);
                //else
                //    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[1]);
                BindDataToGrid(1);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;

            //case "Active":
            //    int nActive = objBanner.UpdateStatus(nID, EnumeType.INACTIVE);
            //    if (nActive > 0)
            //        Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[1]);

            //    else
            //        Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[1]);
            //    BindDataToGrid(Pager1.CurrentIndex);
            //    break;

            //case "Inactive":
            //    int nInactive = objBanner.UpdateStatus(nID, EnumeType.ACTIVE);
            //    if (nInactive > 0)
            //        Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[1]);
            //    else
            //        Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[1]);
            //    BindDataToGrid(Pager1.CurrentIndex);
            //    break;
        }
    }
    protected void RpBanner_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        int nCurrentLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            Label lblType = (Label)e.Item.FindControl("lblType");
            Label lblID = (Label)e.Item.FindControl("lblID");
            Label lblink = (Label)e.Item.FindControl("lblink");

            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            //LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            //LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            // TextBox txtPriority = (TextBox)e.Item.FindControl("txtPriority");
            // Label lblID = (Label)e.Item.FindControl("lblID");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            int nType = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "BannerType"));
            //lblID.Text = ConvertData.ConvertToString(nID);
            //lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            //lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage]);
            lnkDelete.Enabled = true;
            //  lnkActive.Enabled = true;
            switch (nType)
            {
                case 1:
                    lblType.Text = "Home Page";
                    break;
                case 2:
                    lblType.Text = "Events Page";
                    break;
            }

            switch (nCurrentLangID)
            {
                case 1:
                    lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title1"));
                    break;
                case 2:
                    lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title2"));
                    break;
            }
            lblID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            lblink.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Link"));
            //lblDateModified.Text = ConvertData.ConvertIntToDatetimeString(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CreatedDate")));
            //if (nStatus > 0)
            //{
            //    lnkActive.Visible = true;
            //    lnkInactive.Visible = false;
            //}
            //else
            //{
            //    lnkActive.Visible = false;
            //    lnkInactive.Visible = true;
            //}
            string nPriority = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Priority"));

            //  txtPriority.Text = nPriority;




            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_ALERT[nCurrentLangID]);

            //lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));
            //lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nCurrentLangID]);
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "BannerID"));

        }
    }

    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {

        BindDataToGrid(1);
    }

    private void BindDataToGrid(int CurrentPageIndex)
    {

        int nLanguageID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        int nDefaultLanguage = Support.GetDefaultLanguage();

        DataTable dtbBanner = new DataTable();
        Banner objBanner = new Banner();
        dtbBanner = objBanner.GetAll();
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + dtbBanner.Rows.Count + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
        RpBanner.DataSource = dtbBanner;
        RpBanner.DataBind();
    }

 

    #endregion

    #region Click Events


    protected void btnAddnew_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BANNER_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    #endregion

   
}